package com.lin.mr.maizilaundry.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.widget.FrameLayout;
import android.widget.RadioGroup;

import com.lin.mr.maizilaundry.R;
import com.lin.mr.maizilaundry.fragmentorder.FragmentOrderHome;
import com.lin.mr.maizilaundry.fragmentorder.FragmentOrderMe;
import com.lin.mr.maizilaundry.fragmentorder.FragmentOrderMore;

import java.util.ArrayList;
import java.util.List;

public class OrderActivity extends FragmentActivity {
    private List<Fragment> fragmentList;
    private FrameLayout frameLayout;
    private RadioGroup rg_order;
    private FragmentOrderHome home;
    private FragmentOrderMe me;
    private FragmentOrderMore more;
//    private FragmentTransaction ft;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);
        initView();

        rg_order.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                for (int i = 0; i < group.getChildCount(); i++) {
                    if (rg_order.getChildAt(i).getId() == checkedId) {
                        //使RadioGroup和ViewPager同步
                        Fragment fragment = fragmentList.get(i);
                        FragmentTransaction ft = obtainFragmentTransaction(i);
                        fragmentList.get(i).onPause();//启动当前fragment的onPause方法
                        if (fragment.isAdded()) { //为true时表示fragment被添加到了Activity中
                            fragment.onResume(); //启动当前fragment的onResume方法
                        } else {
                            ft.add(R.id.fl_pagerSlide, fragment);
                        }
                        showFragment(i);
                        ft.commit();
                    }

                }
            }
        });
    }


    private void initView() {
        frameLayout = (FrameLayout) findViewById(R.id.fl_pagerSlide);
        rg_order = (RadioGroup) findViewById(R.id.rg_order);
        fragmentList = new ArrayList<>();

        //不知道为什么在属性中设置checked为true之后会一直保存true
//        RadioButton rb = (RadioButton) findViewById(R.id.rb_order);
//        rb.setChecked(true);

        home = new FragmentOrderHome();
        me = new FragmentOrderMe();
        more = new FragmentOrderMore();
        //将3个fragment添加到集合中
        fragmentList.add(home);
        fragmentList.add(me);
        fragmentList.add(more);
        //默认显示第一页
        FragmentTransaction ft = obtainFragmentTransaction(0);
        ft.add(R.id.fl_pagerSlide, fragmentList.get(0));
        ft.commit();
    }

    private void showFragment(int index) {
        for (int i = 0; i < fragmentList.size(); i++) {
            Fragment fragment = fragmentList.get(i);
            FragmentTransaction ft = obtainFragmentTransaction(index);
            if (index == i) {
                ft.show(fragment);
            } else {
                ft.hide(fragment);
            }
            ft.commit();
        }
    }

    private FragmentTransaction obtainFragmentTransaction(int index) {
        FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
        return ft;
    }
}
